package job

import (
	"context"
	"fmt"
	"jk-time/webook/internal/domain"
)

type Executor interface {
	Name() string
	Exec(ctx context.Context, j domain.Job) error
	Register(name string, fn func(ctx context.Context, j domain.Job) error)
}
type LocalFuncExecutor struct {
	funcs map[string]func(ctx context.Context, j domain.Job) error
}

func NewLocalFuncExecutor() *LocalFuncExecutor {
	return &LocalFuncExecutor{funcs: make(map[string]func(ctx context.Context, j domain.Job) error)}
}

func (l LocalFuncExecutor) Name() string {
	return "local"
}
func (l LocalFuncExecutor) Exec(ctx context.Context, j domain.Job) error {
	fn, ok := l.funcs[j.Name]
	if !ok {
		return fmt.Errorf("未知任务，你是否注册了？%s", j.Name)
	}
	return fn(ctx, j)
}
func (l LocalFuncExecutor) Register(name string, fn func(ctx context.Context, j domain.Job) error) {
	l.funcs[name] = fn
}
